package com.lsy.controller;

/**
 * 项目名：springboot02
 * 包名：com.lsy.controller
 * 类名：GroupController
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/6/23
 * 完成时间：
 * 版本：1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsy.dao.GroupMapper;
import com.lsy.dao.UserMapper;
import com.lsy.entity.Group;
import com.lsy.entity.PageResult;
import com.lsy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
public class GroupController {
    @Autowired
    private UserMapper playerMapper;
    @Autowired
    private GroupMapper groupMapper;

    /**
     * 分组方法
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 分组列表
     */
    @GetMapping
    @RequestMapping("/groups")
    public PageResult groupPlayers(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "8") Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", 3).eq("status", 1).eq("promotion", 0);
        List<User> players = playerMapper.selectList(queryWrapper);
        Collections.shuffle(players);
        List<Group> groups = new ArrayList<>();
        for (int i = 0; i < players.size(); i += 2) {
            Group group = new Group(players.get(i).getUserId(), players.get(i + 1).getUserId());
            groups.add(group);
        }
        groupMapper.delete(null);
        groupMapper.insertBatchSomeColumn(groups);
        Page<Group> page = new Page<>(pageNum, pageSize);
        IPage<Group> groupIPage = groupMapper.selectPage(page, null);
        PageResult pageResult = new PageResult();
        pageResult.setGroupList(groupIPage.getRecords());
        pageResult.setCurrentPage(groupIPage.getCurrent());
        pageResult.setTotalPages(groupIPage.getTotal());
        return pageResult;
    }

    /**
     * 分组数方法
     *
     * @return 分组数
     */
    @RequestMapping("/user/getTotalCountGroup")
    @ResponseBody
    public Integer getTotalCountGroup() {
        return Math.toIntExact(groupMapper.selectCount(null));
    }

    /**
     * 展示分组列表方法
     *
     * @param pageNum  当前页号
     * @param pageSize 每页大小
     * @return 分组列表
     */
    @RequestMapping("/showGroups")
    @ResponseBody
    public PageResult showGroups(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "8") Integer pageSize) {
        PageResult pageResult1 = new PageResult();
        Page<Group> page = new Page<>(pageNum, pageSize);
        IPage<Group> groupIPage = groupMapper.selectPage(page, null);
        pageResult1.setGroupList(groupIPage.getRecords());
        pageResult1.setCurrentPage(groupIPage.getCurrent());
        long totalPages = 0;
        if (groupIPage.getTotal() % 8 == 0) {
            totalPages = groupIPage.getTotal() / 8;
        } else if (groupIPage.getTotal() % 8 != 0) {
            totalPages = groupIPage.getTotal() / 8 + 1;
        }
        pageResult1.setTotalPages(totalPages);
        return pageResult1;
    }
}




